package iot.ht.controller.htmenu.service;

import iot.core.data.*;
import iot.core.data.entity.HTMenu;
import iot.core.data.mapper.*;
import iot.ht.controller.htmenu.data.HTMenuDto;
import iot.ht.controller.htmenu.data.HTMenuInsertQto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class HTMenuService extends ServiceImpl<HTMenuMapper, HTMenu> implements IHTMenuService {
    @Autowired
    HTDBMapper htdbMapper;
    @Autowired
    HTPageMapper htPageMapper;
    @Autowired
    HTCtlMapper htCtlMapper;
    @Autowired
    HTActionMapper htActionMapper;
    @Override
    public HTMenuDto InsertInit(HTMenuInsertQto qm){
        var mm=new HTMenuDto();
        mm.setPId(qm.getId());
        mm.setHTPlat_Id(qm.getHTPlat_Id());
        mm.setLX(qm.getLX());
        mm.setImgUrl("layui-icon-spread-left");
        var qw=getBaseMapper().getQueryWrapper();
        qw.eq("PId",qm.getId() );
        var SortNo=getBaseMapper().max(qw,"SortNo");
        SortNo+=10;
        mm.setSortNo(SortNo );
        return mm;
    }
    @Override
    public HTMenuDto Detail(BaseQto qm){
        var m=getBaseMapper().selectById(qm.getId());
        var mm=new HTMenuDto();

        getBaseMapper().entityToDto(m,mm);
        if(m.getHTPage_Id()>0){
            var mpage=htPageMapper.selectById(m.getHTPage_Id() );
            mm.setCode(mpage.getCode());
            mm.setP1(mpage.getP1()  );
            mm.setP2(mpage.getP2()  );
            mm.setP3(mpage.getP3()  );
            mm.setP4(mpage.getP4()  );
        }

        return mm;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int Insert(HTMenuDto mm){

        var m = new  HTMenu();
        getBaseMapper().dtoToEntity(mm,m);

        return getBaseMapper().insert(m,true);

    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public   int Update(HTMenuDto mm){
        var m = getBaseMapper().selectById(mm.getId() );
        getBaseMapper().dtoToEntity(mm,m);

        return getBaseMapper().updateById(m,true);
    }
}
